package com.example.xwquarz.scheduler;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.huanong.it.xwcommon.entity.Weibo;
import com.huanong.it.xwcommon.service.WeiboService;
import com.huanong.it.xwcommon.utils.AliYun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RefreshScope
public class AuditWeiboScheduler {
    @Autowired
    WeiboService weiboService;

    @Scheduled(cron="*/30 * * * * *")
    public void job1() {
        System.out.println("定时任务开始");

        List<Weibo> weiboList = weiboService.all().getData();
        for(Weibo weibo:weiboList){
            if(weibo.getState()!=2)
                continue;
            try {
                String ans= AliYun.chat("微博标题："+weibo.getTitle()+"微博内容："+weibo.getContent());
                if(ans.equals("-1")){
                    weiboService.updateState(weibo.getId(),-1);
                }else if(ans.equals("1")) {
                    weiboService.updateState(weibo.getId(),1);
                }else{
                    weiboService.updateState(weibo.getId(),3);
                }
            }catch (Exception e){
                System.out.println("审核微博失败"+e.getMessage());
            }

        }
    }

}
